/*
 * ScrollBarDelegate.cpp  0.4.0 / Dec 05, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "ScrollBarDelegate.h"

// ScrollBarDelegate :: Protected Constructor. ---------------------------------

/*!
 * \brief Constructs new ScrollBarDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the ScrollBar java object
 */
ScrollBarDelegate::ScrollBarDelegate(JNIEnv *env, jobject obj)
  : ComponentDelegate(env, obj, new QScrollBar(Qt::Vertical))
{
  qScrollBar()->setMaximum(100);
  qScrollBar()->setFocusPolicy(Qt::StrongFocus);
  connectDirect(qScrollBar(), SIGNAL(valueChanged(int)),
                SLOT(valueChangedSlot(int)));
  connectDirect(qScrollBar(), SIGNAL(sliderPressed()),
                SLOT(sliderPressedSlot()));
  connectDirect(qScrollBar(), SIGNAL(sliderReleased()),
                SLOT(sliderReleasedSlot()));
}

// crollBarDelegate :: Native Calls. -------------------------------------------

/*!
 * \brief Sets scroll bar model.
 *
 * \param env the JNIEnv pointer
 * \param minimum the minimum scroll value
 * \param maximum the maximum scroll value
 * \param value the scroll value
 */
void ScrollBarDelegate::setModel(JNIEnv *env, jint minimum, jint maximum,
                                 jint value)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setModel, SWING_ENV_ARG, JINT_ARG(minimum), JINT_ARG(maximum),
                  JINT_ARG(value))
  qScrollBar()->setRange(minimum, maximum);
  qScrollBar()->setValue(value);
}

/*!
 * \brief Sets scroll value.
 *
 * \param env the JNIEnv pointer
 * \param value the scroll value
 */
void ScrollBarDelegate::setValue(JNIEnv *env, jint value)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setValue, SWING_ENV_ARG, JINT_ARG(value))
  qScrollBar()->setValue(value);
}

/*!
 * \brief Sets minimum scroll value.
 *
 * \param env the JNIEnv pointer
 * \param minimum the minimum scroll value
 */
void ScrollBarDelegate::setMinimum(JNIEnv *env, jint minimum)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setMinimum, SWING_ENV_ARG, JINT_ARG(minimum))
  qScrollBar()->setMinimum(minimum);
}

/*!
 * \brief Sets maximum scroll value.
 *
 * \param env the JNIEnv pointer
 * \param maximum the maximum scroll value
 */
void ScrollBarDelegate::setMaximum(JNIEnv *env, jint maximum)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setMaximum, SWING_ENV_ARG, JINT_ARG(maximum))
  qScrollBar()->setMaximum(maximum);
}

/*!
 * \brief Sets scroll bar orientation.
 *
 * \param env the JNIEnv pointer
 * \param orientation the orientation
 */
void ScrollBarDelegate::setOrientation(JNIEnv *env, jint orientation)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setOrientation, SWING_ENV_ARG, JINT_ARG(orientation))
  switch(orientation) {
    case 0:
      qScrollBar()->setOrientation(Qt::Horizontal);
      break;
    case 1:
      qScrollBar()->setOrientation(Qt::Vertical);
      break;
  }
}

/*!
 * \brief Sets scroll unit increment.
 *
 * \param env the JNIEnv pointer
 * \param unitIncrement the unit increment
 */
void ScrollBarDelegate::setUnitIncrement(JNIEnv *env, jint unitIncrement)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setUnitIncrement, SWING_ENV_ARG, JINT_ARG(unitIncrement))
  qScrollBar()->setSingleStep(unitIncrement);
}

/*!
 * \brief Sets scroll block increment.
 *
 * \param env the JNIEnv pointer
 * \param blockIncrement the block increment
 */
void ScrollBarDelegate::setBlockIncrement(JNIEnv *env, jint blockIncrement)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setBlockIncrement, SWING_ENV_ARG, JINT_ARG(blockIncrement))
  qScrollBar()->setPageStep(blockIncrement);
}

// ScrollBarDelegate :: Static Native Calls. -----------------------------------

/*!
 * \brief Initializes the ScrollBarDelegate class.
 *
 * \param env the JNIEnv pointer
 * \param cls the ScrollBar java class
 *
 * \exception FatalError if failed
 */
void ScrollBarDelegate::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_ASSERT(!sm_valueChangedMID);
  if (!INIT_UMID(valueChanged, cls, "(I)V") ||
      !INIT_UMID(valueIsAdjusting, cls, "(Z)V")) {
    FATAL_LOGGED_ERROR("ScrollBarDelegate initialization failed");
  }
}

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the delegate object
 */
jlong ScrollBarDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new ScrollBarDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}

// ScrollBarDelegate :: Native Callbacks. --------------------------------------

/*!
 * \brief Calls \c valueChanged method of the ScrollBar java object.
 *
 * \param value the changed value
 */
void ScrollBarDelegate::valueChanged(jint value)
{
  jniObject()->callVoidMethod(sm_valueChangedMID, value);
}

/*!
 * \brief Calls \c valueIsAdjusting method of the ScrollBar java object.
 *
 * \param adjusting \c true if adjusting
 */
void ScrollBarDelegate::valueIsAdjusting(jboolean adjusting)
{
  jniObject()->callVoidMethod(sm_valueIsAdjustingMID, adjusting);
}

// ScrollBarDelegate :: Protected Member Functions. ----------------------------

/*!
 * \brief \c valueChanged slot.
 *
 * \param value the value
 */
void ScrollBarDelegate::valueChangedSlot(int value)
{
  valueChanged(value);
}

/*!
 * \brief \c sliderPressed slot.
 */
void ScrollBarDelegate::sliderPressedSlot()
{
  valueIsAdjusting(true);
}

/*!
 * \brief \c sliderReleased slot.
 */
void ScrollBarDelegate::sliderReleasedSlot()
{
  valueIsAdjusting(false);
}

// ScrollBarDelegate :: Static Data Members. -----------------------------------

jmethodID ScrollBarDelegate::sm_valueChangedMID = 0;
jmethodID ScrollBarDelegate::sm_valueIsAdjustingMID = 0;
